﻿using NHibernate;
using System;
using Iesi.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace LX.NHibernate.Data
{
    public class RepositoryBase<T> : IRepository<T> where T : class
    {
        public ISessionFactory sessionFactory = DbFactory.GetCurrentSession();

        //public ISession sessionFactory = DbFactory.GetCurrentSession2();
        //如果是用GetCurrentSession2方法、那下面的所有的方法的OpenSession() 不需要了，
        /*直接
                var id = session.Save(entity);
                session.Flush();
                return id;
        */

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public object Insert(T entity)
        {
            using (ISession session = sessionFactory.OpenSession())
            {
                var id = session.Save(entity);
                session.Flush();
                return id;
            }
        }

        public void Update(T entity)
        {
            using (ISession session = sessionFactory.OpenSession())
            {
                session.Update(entity);
                session.Flush();
            }
        }

        public void Delete(T entity)
        {
            using (ISession session = sessionFactory.OpenSession())
            {
                session.Delete(entity);
                session.Flush();
            }
        }

        public T Get(object id)
        {
            using (ISession session = sessionFactory.OpenSession())
            {
                return session.Get<T>(id);
            }
        }

        public T Load(object id)
        {
            using (ISession session = sessionFactory.OpenSession())
            {
                return session.Load<T>(id);
            }
        }

        public List<T> LoadAll()
        {
            using (ISession session = sessionFactory.OpenSession())
            {
                return session.QueryOver<T>().List().ToList();
            }
        }

        public List<T> LoadAllWithPage(out long count, int pageIndex, int pageSize)
        {
            using (ISession session = sessionFactory.OpenSession())
            {
                var result = session.QueryOver<T>();
                count = result.RowCount();

                return result.Skip((pageIndex - 1) * pageSize).Take(pageSize).List().ToList();
            }
        }

        /// <summary>
        /// 分页条件查询和排序
        /// </summary>
        /// <param name="where">过滤条件</param>
        /// <param name="order">排序</param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="Total"></param>
        /// <returns></returns>
        public IList<T> GetListForPage(Expression<Func<T, bool>> where, Expression<Func<T, object>> order, int pageIndex, int pageSize, out int Total)
        {
            using (ISession session = sessionFactory.OpenSession())
            {
                var query = session.QueryOver<T>().Where(where).OrderBy(order).Desc;
                Total = query.List<T>().Count;
                return query.Skip((pageIndex - 1) * pageSize).Take(pageSize).List<T>();
            }
        }
    }
}
